/*
 * Copyright (C) 2012 www.amsoft.cn
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cqyanyu.yanyu.http;


import android.content.Context;
import android.content.Intent;

import com.cqyanyu.yanyu.factory.UserInfoFactory;

import org.json.JSONException;
import org.json.JSONObject;

public class XResultNoData {

    /**
     * 结果code.
     */
    public int code;

    /**
     * 结果 message.
     */
    public String msg;

    public JSONObject data;

    /**
     * 用json构造自己
     *
     * @param json
     */
    public XResultNoData(String json) {
        super();
        try {
            JSONObject jsonObject = new JSONObject(json);
            msg = jsonObject.optString("msg");
            code = jsonObject.optInt("code");
            if (code == 5) {
                UserInfoFactory.startLoginActivity();
            }
            data=jsonObject.optJSONObject("data");
        } catch (JSONException e) {
            e.printStackTrace();
            this.code = -1;
            this.msg = "获取数据失败";
        }

    }

    /**
     * 用json构造自己
     *
     * @param json
     */
    public XResultNoData(String json, Context context) {
        super();
        try {
            JSONObject jsonObject = new JSONObject(json);
            msg = jsonObject.optString("msg");
            code = jsonObject.optInt("code");
            if (code == 5) {
                String activityName = context.getPackageName() + "User.LoginActivity";
                Class clazz = null;
                try {
                    clazz = Class.forName(activityName);
                    Intent intent = new Intent(context, clazz);
                    intent.putExtra("checkState", true);
//                    XToastUtil.showToast(context, "请登录");
                    context.startActivity(intent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
            this.code = -1;
            this.msg = "获取数据失败";
        }

    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }


}
